package com.mui.core
{
	import com.mui.events.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	import flash.utils.*;
	
	public class Library extends EventDispatcher
	{
		private var _enterFrameDispatcher:Sprite;
		private var _embeddedLoaders:Array;
		private var _runtimeLoaders:Array;
		private var _embeddedComplete:Boolean;
		private var _runtimeCompletes:Array;
		private var _runtimeComplete:Boolean;
		private var _name:String;
		private var _bytesLoaded:Number = 0;
		private var _bytesTotal:Number = 0;
		private var _LoaderContext:LoaderContext;
		
		public function Library(name:String)
		{
			this._LoaderContext = new LoaderContext();
			this._name = name;
			this.initialize();
		}
		
		private function initialize() : void
		{
			this._embeddedLoaders = new Array();
			this._runtimeLoaders = new Array();
			this._runtimeCompletes = new Array();
			this._enterFrameDispatcher = new Sprite();
			this._bytesLoaded = 0;
			this._bytesTotal = 0;
		}
		
		public function loadSWF(param1:String) : void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onLoaderProgress, false, 0, true);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoaderComplete, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onIOErrorHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onIOErrorHandler, false, 0, true);
			loader.load(new URLRequest(param1));
			this._runtimeLoaders.push(loader);
			this._runtimeCompletes.push(false);
		}
		
		private function onLoaderProgress(event:ProgressEvent) : void
		{
			checkLoadersProgress();
		}
		
		private function onLoaderComplete(event:Event) : void
		{
			var loader:Loader = Loader(event.target.loader);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.onLoaderProgress);
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onLoaderComplete);
			var len:int = this._runtimeLoaders.length;
			var i:int = 0;
			while (i < len)
			{
				
				if (loader == Loader(this._runtimeLoaders[i]))
				{
					this._runtimeCompletes[i] = true;
					break;
				}
				i++;
			}
			
			checkLoadersProgress();
		}
		
		private function onIOErrorHandler(event:ErrorEvent) : void
		{
			trace("library:" + event.text);
			dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, event.text));
		}
		
		private function checkLoadersProgress() : void
		{
			var loader:Loader;
			var bytesTotal:Number = 0;
			var bytesLoaded:Number = 0;
			var bol:Boolean = true;
			var len:int = this._runtimeLoaders.length;
			var i:int = 0;
			while (i < len)
			{
				
				loader = Loader(this._runtimeLoaders[i]);
				bytesTotal = bytesTotal + loader.contentLoaderInfo.bytesTotal;
				bytesLoaded = bytesLoaded + loader.contentLoaderInfo.bytesLoaded;
				if (!this._runtimeCompletes[i])
				{
					bol = false;
				}
				i++;
			}
			this._bytesLoaded = bytesLoaded;
			this._bytesTotal = bytesTotal;
			if (bol)
			{
				this._runtimeComplete = true;
				dispatchEvent(new LibraryEvent(LibraryEvent.LOAD_COMPLETE, false, false));
			}
			else
			{
				dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, bytesLoaded, bytesTotal));
			}
		}
		
		public function getDefinition(param1:String) : Class
		{
			
			return null;
		}
	}
}